//
//  Fraction.m
//  HomeWork 02 - 完成分数练习 及 预习 NSString
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
- (instancetype)initWithNume:(NSInteger)nume demo:(NSInteger)deno {
    self = [super init];
    if (self) {
        _numerator = nume;
        _denorminator = deno;
    }
    
    return self;
    
    
}
+ (instancetype)fractionWithNum:(NSInteger)nume demo:(NSInteger)deno {
    
    return [[[Fraction alloc]initWithNume:nume demo:deno]autorelease];
    
}

- (void)compareFraction:(Fraction *)frac {
    
    NSInteger num1 = self.numerator * frac.denorminator;
    NSInteger num2 = self.denorminator * frac.numerator;
    if (num1 > num2) {
        NSLog(@"第一个数大");
        
    }
    else if (num1 < num2) {
        NSLog(@"第二个数大");
    }
    
}

- (void)reduce {
    long m = self.numerator;
    long n = self.denorminator;
    long temp = 0;
    while (n % m != 0) {
        temp = n % m;
        n = m;
        m = temp;
    }
    
    self.numerator /= m;
    self.denorminator /= m;
}

- (Fraction *)addFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.denorminator + self.denorminator * frac.numerator;
    NSInteger newDem = self.denorminator * frac .denorminator;
    Fraction *newFrac = [Fraction fractionWithNum:newNum demo:newDem];
    
    
    
    return newFrac;
    
    
}
- (Fraction *)minusFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.denorminator - self.denorminator * frac. numerator;
    NSInteger newDem = self.denorminator * frac.denorminator;
    Fraction *newFrac = [Fraction fractionWithNum:newNum demo:newDem];
    return newFrac;
    
}

- (Fraction *)mulFraction:(Fraction *)frac{
    NSInteger newNum = self.numerator * frac.numerator;
    NSInteger newDem = self.denorminator * frac.denorminator;
    Fraction *newFrac = [Fraction fractionWithNum:newNum demo:newDem];
    return newFrac;
    
    
}
- (Fraction *)divisionFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.denorminator;
    NSInteger newDem = self.denorminator * frac.numerator;
    Fraction *newFrac = [Fraction fractionWithNum:newNum demo:newDem];
    return newFrac;
    
}


@end
